package com.wending.main_page;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.wending.R;
import com.wending.config.Constant;
import com.wending.view.ProgressWebView;

/**
 * Created by ${张英杰} on 2017/2/5.
 */

public class CenterActivity extends AppCompatActivity {
    ProgressWebView mWebView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_fragment);
        ProgressWebView mWebView = (ProgressWebView) findViewById(R.id.wv);
        String weburl = "http://www.wending888.com/wending/system/user_QtMyAdmin.action";
        WebSettings settings = mWebView.getSettings();
//        设置网页布局类型：
//        *  1、LayoutAlgorithm.NARROW_COLUMNS ： 适应内容大小
//        *  2、LayoutAlgorithm.SINGLE_COLUMN: 适应屏幕，内容将自动缩放  有些网页混乱！！！飞起
//        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setJavaScriptEnabled(true);
        settings.setAppCacheEnabled(false);//设置缓存
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        String cloadUrl=weburl + "?data=" + System.currentTimeMillis() / 1000;
        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        if (!TextUtils.isEmpty(Constant.localCookie)){
            cookieManager.setCookie(cloadUrl,Constant.localCookie);
            CookieSyncManager.getInstance().sync();
        }

        //WebView加载web资源
        Log.i("webFragment===webUrl ",cloadUrl);
        mWebView.loadUrl(cloadUrl);
//                //加载html
//               webView.loadData(weburl,"text/html","utf-8");//在有些手机上会出现乱码，将方法变成
//                webView.loadDataWithBaseURL(null,weburl,"text/html","utf-8",null);//就可以解决这个
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                Log.i("webviewUrl===", url);
                view.loadUrl(url);
                return true;
            }
        });
    }
}
